var $table = $("#driverTab");
$(function () {
    initTable();
    //查询事件
    $("#queryDriverBtn").click(function () {
        $table.bootstrapTable('refresh');
    });
    //增加按钮
    $("#addBut").click(function () {
    	initDriverEdit(null);
    });
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../driver/getDriverInfoByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "编号",
                field: "driverNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "用户名",
                field: "driverName",
                align: 'center',
                valign: 'middle'

            },
            {
                title: "真实姓名",
                field: "driverRealName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "年龄",
                field: "driverAge",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "驾照",
                field: "driverLicence",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "身份证",
                field: "driverIdcard",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "手机号",
                field: "driverPhone",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "时间",
                field: "driverInsTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var d = '';
                    if (row.driverIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteDriver(\'' + row.driverNum + '\',\'' + row.driverName + '\')"><i class="fa fa-trash-o"></i> 删除 </button> ';
                    }
                    return d;
                }
            }
        ]
    });
}


//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		driverRealName: $.trim($('#driverRealName').val()),
		driverPhone: $.trim($('#driverPhone').val()),
	};
	return temp;
}

//编辑公司信息
function initDriverEdit(id){
    var title;
    if(id){
        title = '编辑司机';
    }else{
        title = '新增司机';
    }
    var index = layer.open({
        type: 2,
        title: title,
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        area : ['500px' , '550px'],
        content: '../driver/edit_driver.htm?id='+id,
        success: function(layero, index){
        }
    });
//    layer.full(index);
}

//设置司机状态
function deleteDriver(driverNum, driverName) {
    layer.confirm('你确定要删除【' + driverName + '】吗？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../driver/delDriverInfo.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'driverNum': driverNum},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../driver/getDriverInfoByPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}